package server

import (
	"git.oschina.net/dddailing/drsys/backend/model"
	"github.com/dailing/levlog"
)

func (app *App) RequireToken(j *JsonType) (*JsonType, int) {
	token := j.GetString(RequestKeyToken)
	if token == "" {
		return ResponseError(401, "No Token")
	}
	per, err := model.ParseToken(token)
	levlog.E(err)
	if per == nil || err != nil || per.UserName == "" {
		return ResponseError(401, "Token Error")
	}
	j.Set(RequestKeyToken, per)
	return NoResponse()
}

func SetOwner(j *JsonType) (*JsonType, int) {
	obj := j.GetReqObj()
	token := j.GetReqToken()
	if owner, ok := obj.(model.ObjWithOwnerInterface); ok && owner != nil {
		owner.SetOwner(token.UserID)
	} else {
		 levlog.Error("error get owner interface")
		return ResponseError(500, "error get owner interface")
	}
	return NoResponse()
}

func CheckOwner(j *JsonType) (*JsonType, int) {
	obj := j.GetReqObj()
	token := j.GetReqToken()
	if owner, ok := obj.(model.ObjWithOwnerInterface); ok && owner != nil {
		//levlog.Warning(owner.GetOwner(), "VS.", token.UserID)
		if owner.GetOwner() == token.UserID {
			return NoResponse()
		} else {
			return ResponseError(401, "object not owned by you")
		}
	}
	return ResponseError(500, "error get owner interface")
}

func InsertDB(j *JsonType) (*JsonType, int) {
	// this middleware needs body to obj
	obj := j.GetReqObj()
	var (
		dbobj model.DBDInterface
		ok    bool
		err   error
	)
	if obj == nil {
		return ResponseError(500, "Object Passing Failed")
	}
	if dbobj, ok = obj.(model.DBDInterface); ok && dbobj != nil {
		err = dbobj.Insert()
	} else {
		panic("method not implemented")
	}
	levlog.E(err)
	if err != nil {
		return ResponseError(500, "Error Inserting to db")
	}
	return ResponseAnyToJson(obj)
}

func DeleteDB(j *JsonType) (*JsonType, int) {
	// this middleware needs body to obj
	obj := j.GetReqObj()
	if obj == nil {
		return ResponseError(500, "Failed to get Object")
	}
	if deleteField, ok := obj.(model.DBDInterface); ok {
		err := deleteField.Delete()
		if err != nil {
			levlog.E(err)
			return ResponseError(500, "Error Inserting to db")
		}
	} else {
		panic("method not implemented")
	}
	return ResponseAnyToJson(obj)
}

func UpdateDB(j *JsonType) (*JsonType, int) {
	// this middleware needs body to obj
	var err error
	obj := j.GetReqObj()
	if obj == nil {
		return ResponseError(500, "Object Passing Failed")
	}
	if dbobj, ok := obj.(model.DBDInterface); ok && dbobj != nil {
		err = dbobj.Update()
	} else {
		panic("method not implemented")
	}
	levlog.E(err)
	if err != nil {
		return ResponseError(500, "Error Inserting to db")
	}
	return ResponseSucc()
}

func GetById(j *JsonType) (*JsonType, int) {
	var err error
	obj := j.GetReqObj()
	if obj == nil {
		return ResponseError(500, "Object Passing Failed")
	}
	if dbobj, ok := obj.(model.DBDInterface); ok && dbobj != nil {
		err = dbobj.Get()
	} else {
		panic("method not implemented")
	}
	if err != nil {
		levlog.E(err)
		return ResponseError(500, "Error Inserting to db")
	}
	return ResponseAnyToJson(obj)
}
